package com.lxh.lxhcrm.model.service;

import com.lxh.lxhcrm.model.mappers.productManagement.ReplenishMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ReplenishService {
    @Resource
    ReplenishMapper replenishMapper;
    /**
     * 月度数量统计
     * @param year
     * @return
     */
    public List<Map<String,Object>> firstChart(String year){
        //获得fa_id is null 的有哪些
        List<Integer> listId = new ArrayList();
        List<Map<String,Object>> list1 = replenishMapper.findclassily();
        List<Map<String,Object>> listItem = null;
        String pro_classily_name;
        //新生成一个List<Map<String,Object>>
        List<Map<String,Object>> newList = new ArrayList();
        //取出fa_id is null 的数据的id
        for (Map map:list1) {
            Integer index = (Integer)(map.get("pro_classily_id"));
            listId.add(index);
        }
        //根据id查出这个类里面的商品每个月的数量
        for (Integer ob:listId) {
            double sum = 0;
            List<Double> arrList = new ArrayList();
            Map<String,Object> maps = new HashMap<>();
            double[] dou = new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            //查出name
            pro_classily_name = replenishMapper.findclassilyName(ob);
            listItem = replenishMapper.firstChart(year,ob);
            if(!listItem.isEmpty()){
                for(Map map:listItem){
                    Integer mon = (Integer)(map.get("mon"));
                    Double item = (Double) (map.get("sums"));
                    sum = sum + item;
                    dou[mon-1] = item;
                }
                for (double d: dou) {
                    arrList.add(d);
                }
            }else{
                for (double d: dou) {
                    arrList.add(d);
                }
            }
            maps.put("proCName",pro_classily_name);
            maps.put("arrData",arrList);
            maps.put("sum",sum);
            newList.add(maps);
        }
        return newList;
    }

    /**
     * 月度金额统计
     * @param year
     * @return
     */
    public List<Map<String,Object>> firstCharts(String year){
        List<Integer> listId = new ArrayList();
        List<Map<String,Object>> list1 = replenishMapper.findclassily();
        List<Map<String,Object>> listItem = null;
        String pro_classily_name;
        //新生成一个List<Map<String,Object>>
        List<Map<String,Object>> newList = new ArrayList();
        //取出fa_id is null 的数据的id
        for (Map map:list1) {
            Integer index = (Integer)(map.get("pro_classily_id"));
            listId.add(index);
        }
        //根据id查出这个类里面的商品每个月的数量
        for (Integer ob:listId) {
            double sum = 0;
            List<Double> arrList = new ArrayList();
            Map<String,Object> maps = new HashMap<>();
            double[] dou = new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            //查出name
            pro_classily_name = replenishMapper.findclassilyName(ob);
            listItem = replenishMapper.firstChart1(year,ob);
            if(!listItem.isEmpty()){
                for(Map map:listItem){
                    Integer mon = (Integer)(map.get("mon"));
                    Double item = (Double) (map.get("sums"));
                    sum = sum + item;
                    dou[mon-1] = item;
                }
                for (double d: dou) {
                    arrList.add(d);
                }
            }else{
                for (double d: dou) {
                    arrList.add(d);
                }
            }
            maps.put("proCName",pro_classily_name);
            maps.put("arrData",arrList);
            maps.put("sum",sum);
            newList.add(maps);
        }
        return newList;
    }

    /**
     * 季度数据统计
     * @param year
     * @return
     */
    public List<Map<String,Object>> firstQChart(String year){
        List<Integer> listId = new ArrayList();
        List<Map<String,Object>> list1 = replenishMapper.findclassily();
        List<Map<String,Object>> listItem = null;
        String pro_classily_name;
        //新生成一个List<Map<String,Object>>
        List<Map<String,Object>> newList = new ArrayList();
        //取出fa_id is null 的数据的id
        for (Map map:list1) {
            Integer index = (Integer)(map.get("pro_classily_id"));
            listId.add(index);
        }
        //根据id查出这个类里面的商品每个月的数量
        for (Integer ob:listId) {
            double sum = 0;
            List<Double> arrList = new ArrayList();
            Map<String,Object> maps = new HashMap<>();
            double[] dou = new double[]{0, 0, 0, 0};
            //查出name
            pro_classily_name = replenishMapper.findclassilyName(ob);
            listItem = replenishMapper.firstQChart(year,ob);
            if(!listItem.isEmpty()){
                for(Map map:listItem){
                    Integer mon = (Integer)(map.get("mon"));
                    Double item = (Double) (map.get("sums"));
                    sum = sum + item;
                    dou[mon-1] = item;
                }
                for (double d: dou) {
                    arrList.add(d);
                }
            }else{
                for (double d: dou) {
                    arrList.add(d);
                }
            }
            maps.put("proCName",pro_classily_name);
            maps.put("arrData",arrList);
            maps.put("sum",sum);
            newList.add(maps);
        }
        return newList;
    }

    /**
     * 季度金额统计
     * @param year
     * @return
     */
    public List<Map<String,Object>> firstQCharts(String year){
        List<Integer> listId = new ArrayList();
        List<Map<String,Object>> list1 = replenishMapper.findclassily();
        List<Map<String,Object>> listItem = null;
        String pro_classily_name;
        //新生成一个List<Map<String,Object>>
        List<Map<String,Object>> newList = new ArrayList();
        //取出fa_id is null 的数据的id
        for (Map map:list1) {
            Integer index = (Integer)(map.get("pro_classily_id"));
            listId.add(index);
        }
        //根据id查出这个类里面的商品每个月的数量
        for (Integer ob:listId) {
            double sum = 0;
            List<Double> arrList = new ArrayList();
            Map<String,Object> maps = new HashMap<>();
            double[] dou = new double[]{0, 0, 0, 0};
            //查出name
            pro_classily_name = replenishMapper.findclassilyName(ob);
            listItem = replenishMapper.firstQChart1(year,ob);
            if(!listItem.isEmpty()){
                for(Map map:listItem){
                    Integer mon = (Integer)(map.get("mon"));
                    Double item = (Double) (map.get("sums"));
                    sum = sum + item;
                    dou[mon-1] = item;
                }
                for (double d: dou) {
                    arrList.add(d);
                }
            }else{
                for (double d: dou) {
                    arrList.add(d);
                }
            }
            maps.put("proCName",pro_classily_name);
            maps.put("arrData",arrList);
            maps.put("sum",sum);
            newList.add(maps);
        }
        return newList;
    }

    /**
     * 第二张图为圆饼时的service代码
     * @param name
     * @param monNum
     * @param timeValue
     * @param year
     * @param changeValue
     * @return
     */
    public List<Map<String, Object>> seccondChart(String name, Integer monNum,Integer timeValue,String year,Integer changeValue) {
        Integer id = replenishMapper.findclassilyId(name);
        List<Map<String,Object>> listItem = new ArrayList<>();
        if(timeValue == 1 && changeValue == 1){
            /*月度数量统计*/
            System.out.println("用了这方法12");
            listItem = replenishMapper.seccondChartPie(monNum,id,year);
        }else if(timeValue == 1 && changeValue == 2){
            /*月度金额统计*/
            System.out.println("用了这方法22");
            listItem = replenishMapper.seccondMChartPie(monNum,id,year);
        }else if(timeValue == 2 && changeValue == 1){
            /*季度数量统计*/
            System.out.println("用了这方法32");
            listItem = replenishMapper.selectQMChart(monNum,id,year);
        }else if(timeValue == 2 && changeValue == 2){
            /*季度金额统计*/
            System.out.println("用了这方法42");
            listItem = replenishMapper.selectQMMChart(monNum,id,year);
        }else if(timeValue == 3 && changeValue == 1){
            listItem = firstChart(year);
        }else if(timeValue == 3 && changeValue == 2){
            listItem = firstCharts(year);
        }
        return listItem;
    }
}
